﻿package igz.fleaxy.ui.table 
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import igz.fleaxy.css.CssManager;
	import igz.fleaxy.Fleaxy;
	import igz.fleaxy.pattern.Dto;
	import igz.fleaxy.util.DataTableUtil;
	import igz.fleaxy.util.ObjectUtil;
	
	/**
	 * @author Intelygenz | www.intelygenz.com
	 */
	public class TableRowUi extends Sprite {

		protected var _TableUi:TableUi;
		public function get $TableUi() : TableUi {
			return _TableUi;
		}

		protected var _Mouse_RollOver:Function;
		protected var _Mouse_RollOut:Function;
		protected var _Mouse_Click:Function;

		protected var _TableFieldUi:Array;
		public function get $TableFieldUi() : Array {
			return _TableFieldUi;
		}

		protected var _Settings : Object;
		public function get $Settings() : Object {
			return _Settings;
		}
		
		protected var _DataRow : Array;
		public function get $DataRow () : Array {
			return _DataRow;
		}
		
		private var _RowWidth : Number;

		/**
		 * 
		 * @param	p_tableUi   tableUi donde se añadirá esta fila
		 * @param	p_events   Eventos que se añadiran a esta fila
		 *                   { rollOver:function, rollOut:function, click:function } reciben el TableRowUi como parámetro
		 * 
		 * @param	p_settings 	{
								"styleName" : ""
								,"fieldMaxLines" : 4
								,"selected" : false
								,"headerRow" : false
								,"alignCenter" : false
								}
		 */
		public function TableRowUi ( p_tableUi:TableUi, p_events:Object, p_settings:Object ) {
			_Settings = {
				 "styleName" : ""
				,"fieldMaxLines" : 1
				,"selected" : false
				,"headerRow" : false
			};
			ObjectUtil.$Merge( p_settings, _Settings );
			_TableUi = p_tableUi;

			if ( p_events.hasOwnProperty("rollOver") ) {
				_Mouse_RollOver = p_events["rollOver"];
				addEventListener(MouseEvent.ROLL_OVER, _Mouse_OnRollOver);
			}
			if ( p_events.hasOwnProperty("rollOut") ) {
				_Mouse_RollOut = p_events["rollOut"];
				addEventListener(MouseEvent.ROLL_OUT, _Mouse_OnRollOut);
			}
			if ( p_events.hasOwnProperty("click") ) {
				_Mouse_Click = p_events["click"];
				addEventListener(MouseEvent.CLICK, _Mouse_OnClick);
				this.buttonMode    = true;
				this.tabEnabled    = false;
				this.useHandCursor = true;
			}
		}
		
		public function $Load( p_columns:Array, p_dataRow:Array = null ) : void {
			_DataRow = p_dataRow;
			
			var fieldUi:TableFieldUi;
			if ( _TableFieldUi != null ) {
				for each ( fieldUi in _TableFieldUi ) {
					removeChild( fieldUi );
				}
			}
			_TableFieldUi = new Array();

			var i:uint;
			var column:Object;
			for (i = 0; i < p_columns.length; i++ ) {
				column = p_columns[i];
				fieldUi = new TableFieldUi(this
										  , { "styleName" : _Settings["styleName"]
										    , "maxLines"  : _TableUi.$Settings["fieldMaxLines"]
											, "sortField" : column["order"]
											, "textAlign" : column["textAlign"]
										    }
										  );
				fieldUi.$Load( p_dataRow, column["inner"] );
				
				_TableFieldUi.push( fieldUi );
			}
		}
		
		public function $Draw():void {
			_RowWidth = 0;
			
			var i:uint;
			var tableFieldUi : TableFieldUi;
			
			var nextX : uint = 0;
			var headerColumn:Object;
			for ( i = 0; i < _TableFieldUi.length; i++ ) {
				headerColumn = _TableUi.$HeaderColumns[i];
				tableFieldUi = _TableFieldUi[i] as TableFieldUi;
				tableFieldUi.visible = headerColumn["visible"];

				if ( headerColumn["visible"] ) {
					tableFieldUi.$Draw( headerColumn["width"] );
					tableFieldUi.x = nextX;
					nextX += headerColumn["width"];
					if ( !tableFieldUi.parent ) {
						addChild( tableFieldUi );
					}
				}
				else if ( tableFieldUi.parent ) {
					removeChild( tableFieldUi );
				}
			}

			_RowWidth = nextX;
			if ( _RowWidth < _TableUi.width ) {
				_RowWidth = _TableUi.width - _TableUi._DataUiScrollBarV.width;
			}

			_DrawBg();
		}

		public function $ApplyStyle ( p_style:String ) : void {
			_Settings["styleName"] = p_style;
			
			var fieldUi : TableFieldUi;
			var f:uint; var F:uint = _TableFieldUi.length;
			for ( f = 0; f < F; f++ ) {
				fieldUi = _TableFieldUi[f] as TableFieldUi;
				fieldUi.$Settings = { "styleName": p_style };
			}
			
			_DrawBg();
		}

		private function _DrawBg() : void {
			var bg:String = Fleaxy.$Current.$CssManager.$GetStyleValue(_Settings["styleName"], CssManager.$STYLE_BACKGROUND);
			if ( bg != null ) {
				graphics.clear();
				graphics.beginFill( Number(bg) );
				graphics.drawRect(0, 0, _RowWidth, height);
				graphics.endFill();
			}
		}

		private function _Mouse_OnRollOver(p_mouseEvent:MouseEvent) : void {
			_Mouse_RollOver(this);
		}

		private function _Mouse_OnRollOut(p_mouseEvent:MouseEvent) : void {
			_Mouse_RollOut(this);
		}

		private function _Mouse_OnClick(p_mouseEvent:MouseEvent) : void {
			_Mouse_Click(this);
		}

		override public function get width():Number { return _RowWidth; }
	}
	
}